package com.ai.chat.service.impl;

import com.ai.chat.entity.Conversation;
import com.ai.chat.mapper.ConversationMapper;
import com.ai.chat.service.ConversationService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ConversationServiceImpl implements ConversationService {

    @Autowired
    private ConversationMapper conversationMapper;

    @Override
    public Conversation createConversation(Long userId, String title) {
        Assert.notNull(userId, "用户ID不能为空");
        Assert.hasText(title, "对话标题不能为空");

        Conversation conversation = new Conversation();
        conversation.setUserId(userId);
        conversation.setTitle(title);
        conversation.setCreateTime(LocalDateTime.now());
        conversation.setUpdateTime(LocalDateTime.now());
        conversation.setIsDeleted(0);

        conversationMapper.insert(conversation);
        return conversation;
    }

    @Override
    public boolean deleteConversation(Long userId, Long conversationId) {
        Assert.notNull(userId, "用户ID不能为空");
        Assert.notNull(conversationId, "对话ID不能为空");

        // 验证对话是否存在且属于当前用户
        Conversation conversation = conversationMapper.selectById(conversationId);
        Assert.notNull(conversation, "对话不存在");
        Assert.isTrue(conversation.getUserId().equals(userId), "无权删除此对话");

        // 软删除
        UpdateWrapper<Conversation> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", conversationId)
                     .eq("user_id", userId)
                     .set("is_deleted", 1)
                     .set("update_time", LocalDateTime.now());

        return conversationMapper.update(null, updateWrapper) > 0;
    }

    @Override
    public List<Conversation> getUserConversations(Long userId) {
        Assert.notNull(userId, "用户ID不能为空");
        return conversationMapper.selectByUserId(userId);
    }

    @Override
    public Conversation updateConversationTitle(Long userId, Long conversationId, String newTitle) {
        Assert.notNull(userId, "用户ID不能为空");
        Assert.notNull(conversationId, "对话ID不能为空");
        Assert.hasText(newTitle, "新标题不能为空");

        // 验证对话是否存在且属于当前用户
        Conversation conversation = conversationMapper.selectById(conversationId);
        Assert.notNull(conversation, "对话不存在");
        Assert.isTrue(conversation.getUserId().equals(userId), "无权更新此对话");

        // 更新标题
        conversation.setTitle(newTitle);
        conversation.setUpdateTime(LocalDateTime.now());
        conversationMapper.updateById(conversation);

        return conversation;
    }

    @Override
    public Conversation getConversationById(Long conversationId) {
        Assert.notNull(conversationId, "对话ID不能为空");

        Conversation conversation = conversationMapper.selectById(conversationId);
        Assert.notNull(conversation, "对话不存在");
        Assert.isTrue(conversation.getIsDeleted() == 0, "对话已被删除");

        return conversation;
    }
}